package com.fzhucc.base

import io.swagger.annotations.ApiModelProperty
import org.springframework.data.domain.Page

/**
 * @author KQiang Weng
 * @since 2022/07/05 10:32
 */
data class PageDTO<T>(
    @ApiModelProperty(value = "内容总数")
    val totalElements: Long,
    @ApiModelProperty(value = "页内容")
    val content: List<T>
) {
    companion object {
        @JvmStatic
        fun <T> by(page: Page<T>): PageDTO<T> {
            return PageDTO(page.totalElements, page.content)
        }

        @JvmStatic
        fun <T> by(content: List<T>?, totalElement: Long): PageDTO<T> {
            return PageDTO(totalElement, content ?: emptyList())
        }

        fun <T> Page<T>.toDTO(): PageDTO<T> {
            return PageDTO(totalElements, content)
        }
    }
}